import uuid

from rest_framework import serializers
from orders.models import Order1, Order2, Order3
import snowflake.client
from dingxiang_doctor.utils.myredis import r

class Order1Serializer(serializers.ModelSerializer):
    doctor_name = serializers.CharField(source="doctor.name", read_only=True)
    doctor_department = serializers.CharField(source="doctor.department.name", read_only=True)

    class Meta:
        model = Order1
        fields = "__all__"

    def create(self, validated_data):
        user = validated_data.get("user")
        doctor = validated_data.get("doctor")
        price = validated_data.get("price")
        detail = validated_data.get("detail")

        # 使用雪花算法生成订单id，同时防止时钟回拨情况
        last_id = int(r.get_str("order_id"))
        for i in range(3):
            order_id = snowflake.client.get_guid()
            if order_id > last_id:
                break
            if i == 2:
                # 通过脚本调整服务器时间
                pass
        order_id = snowflake.client.get_guid()
        r.set_str("order_id", order_id)

        order = Order1.objects.create(
            # 保证每一个订单号唯一
            id=order_id,
            user=user,
            doctor=doctor,
            price=price,
            detail=detail,
        )
        return order


class Order2Serializer(serializers.ModelSerializer):
    doctor_name = serializers.CharField(source="doctor.name", read_only=True)
    doctor_department = serializers.CharField(source="doctor.department.name", read_only=True)

    class Meta:
        model = Order2
        fields = "__all__"

    def create(self, validated_data):
        user = validated_data.get("user")
        doctor = validated_data.get("doctor")
        price = validated_data.get("price")
        detail = validated_data.get("detail")

        # 使用雪花算法生成订单id，同时防止时钟回拨情况
        last_id = int(r.get_str("order_id"))
        for i in range(3):
            order_id = snowflake.client.get_guid()
            if order_id > last_id:
                break
            if i == 2:
                # 通过脚本调整服务器时间
                pass
        order_id = snowflake.client.get_guid()
        r.set_str("order_id", order_id)

        order = Order2.objects.create(
            # 保证每一个订单号唯一
            id=order_id,
            user=user,
            doctor=doctor,
            price=price,
            detail=detail,
        )
        return order


class Order3Serializer(serializers.ModelSerializer):
    doctor_name = serializers.CharField(source="doctor.name", read_only=True)
    doctor_department = serializers.CharField(source="doctor.department.name", read_only=True)

    class Meta:
        model = Order3
        fields = "__all__"

    def create(self, validated_data):
        user = validated_data.get("user")
        doctor = validated_data.get("doctor")
        price = validated_data.get("price")
        detail = validated_data.get("detail")

        # 使用雪花算法生成订单id，同时防止时钟回拨情况
        last_id = int(r.get_str("order_id"))
        for i in range(3):
            order_id = snowflake.client.get_guid()
            if order_id > last_id:
                break
            if i == 2:
                # 通过脚本调整服务器时间
                pass
        order_id = snowflake.client.get_guid()
        r.set_str("order_id", order_id)

        order = Order3.objects.create(
            # 保证每一个订单号唯一
            id=order_id,
            user=user,
            doctor=doctor,
            price=price,
            detail=detail,
        )
        return order
